package org.jfsm.log;

import java.io.PrintStream;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class StreamLogger extends Logger {
    PrintStream out;
    
    BlockingQueue<String> queue = new LinkedBlockingQueue<String>();

    public StreamLogger(PrintStream outStream, boolean enabled) {
    	super(enabled);
        this.out = outStream;
        Thread printer = new Thread() {
            @Override
            public void run() {
                for(;;) {
                    try {
                        out.print( queue.take() );
                        
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        printer.start();
    }

    public void log(String str) {
        queue.offer(str);
    }
}
